# Test script for User Management System API
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host "Testing User Management System API" -ForegroundColor Green
Write-Host "=========================================" -ForegroundColor Cyan

# Set base URL
$BASE_URL = "http://localhost:8900/api"

# Test health check
Write-Host "`nTesting health check..." -ForegroundColor Green
try {
    $healthResponse = Invoke-RestMethod -Uri "http://localhost:8900/health" -Method GET
    Write-Host "✓ Health check successful" -ForegroundColor Green
    Write-Host "  Status: $($healthResponse.status)" -ForegroundColor Gray
} catch {
    Write-Host "✗ Health check failed: $_" -ForegroundColor Red
    Write-Host "Please ensure the service is running" -ForegroundColor Yellow
    exit 1
}

# Test create user
Write-Host "`nTesting create user..." -ForegroundColor Green
try {
    $userData = @{
        "name" = "Test User"
        "age" = 25
    }
    $createResponse = Invoke-RestMethod -Uri "$BASE_URL/users" -Method POST -Body ($userData | ConvertTo-Json) -ContentType "application/json"
    Write-Host "✓ User creation successful" -ForegroundColor Green
    Write-Host "  User ID: $($createResponse.id)" -ForegroundColor Gray
    $userId = $createResponse.id
} catch {
    Write-Host "✗ User creation failed: $_" -ForegroundColor Red
}

# Test get single user (if created successfully)
if ($userId) {
    Write-Host "`nTesting get single user..." -ForegroundColor Green
    try {
        $getResponse = Invoke-RestMethod -Uri "$BASE_URL/users/$userId" -Method GET
        Write-Host "✓ Get user details successful" -ForegroundColor Green
        Write-Host "  User name: $($getResponse.name)" -ForegroundColor Gray
    } catch {
        Write-Host "✗ Get user details failed: $_" -ForegroundColor Red
    }

    # Test update user
    Write-Host "`nTesting update user..." -ForegroundColor Green
    try {
        $updateData = @{
            "name" = "Updated User"
            "age" = 30
        }
        $updateResponse = Invoke-RestMethod -Uri "$BASE_URL/users/$userId" -Method PUT -Body ($updateData | ConvertTo-Json) -ContentType "application/json"
        Write-Host "✓ User update successful" -ForegroundColor Green
        Write-Host "  New user name: $($updateResponse.name)" -ForegroundColor Gray
    } catch {
        Write-Host "✗ User update failed: $_" -ForegroundColor Red
    }

    # Test delete user
    Write-Host "`nTesting delete user..." -ForegroundColor Green
    try {
        $deleteResponse = Invoke-RestMethod -Uri "$BASE_URL/users/$userId" -Method DELETE
        Write-Host "✓ User deletion successful" -ForegroundColor Green
    } catch {
        Write-Host "✗ User deletion failed: $_" -ForegroundColor Red
    }
}

# Test get all users
Write-Host "`nTesting get all users..." -ForegroundColor Green
try {
    $listResponse = Invoke-RestMethod -Uri "$BASE_URL/users" -Method GET
    Write-Host "✓ Get users list successful" -ForegroundColor Green
    Write-Host "  User count: $($listResponse.Count)" -ForegroundColor Gray
} catch {
    Write-Host "✗ Get users list failed: $_" -ForegroundColor Red
}

Write-Host "`n=========================================" -ForegroundColor Cyan
Write-Host "API Testing Completed" -ForegroundColor Green
Write-Host "=========================================" -ForegroundColor Cyan